package com.wangyonghao.harmonyocrs.utils;


import okhttp3.*;

import java.io.File;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class OCRUtils {
//    private static String preFix = "http://43.142.11.226:8080/ocrs-server-0.0.1-SN";
    private static String preFix = "http://192.168.43.77:9090";

    private static OkHttpClient client = new OkHttpClient();
    private static ExecutorService executorService = Executors.newFixedThreadPool(3);


    public static String simpleImg(File file) {
        String resStr = null;
        try {
            //提取到文件名
            //转换成文件流

            //接收文件的服务器地址
//            String uploadURL = "http://" + host + ":" + port + "/" + appName + "/ocr/simpleImg";
//            String uploadURL = "http://43.142.11.226:8080/ocrs-server-0.0.1-SN/ocr/simpleImg";
            String uploadURL = preFix+"/ocr/simpleImg";

            MultipartBody multipartBody = new MultipartBody.Builder()
//                    .setType(MultipartBody.FORM)
                    .addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("application/octet-stream"), file))
                    .build();

            Request request = new Request.Builder()
                    .addHeader("connection", "keep-alive")
                    .addHeader("content-type", "multipart/form-data")
                    .addHeader("keep-alive", "timeout=20")
                    .addHeader("transfer-encoding", "chunked")
                    .url(uploadURL)
                    .post(multipartBody)
                    .build();
            Response response = client.newCall(request).execute();
            resStr = response.body().string();
//            ToastUtils.showDialog(res, "" + (response.body() == null));
//                res.setText("请求成功" );
//            ToastUtils.showDialog(res, "请求成功" + resStr);

//            Call call = client.newCall(request);
//            call.enqueue(new Callback() {
//                @Override
//                public void onFailure(@NotNull Call call, @NotNull IOException e) {
//                    ToastUtils.showDialog(res, "请求失败");
//                }
//
//                @Override
//                public void onResponse(Call call, @NotNull Response response) throws IOException {
//                    ToastUtils.showDialog(res, "请求成功");
//                    String s = response.body().string();
//                    ToastUtils.showDialog(res, "请求成功" + s);
//                    //此处根据服务器返回的参数转换，这里返回的是JSON格式
////                JSONObject output = JSON.parseObject(s);
////                JSONArray body = output.getJSONArray("body");
////                resStr = body.get(0) + "";
//                }
//            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return resStr;
    }

    public static String doOCR(File file) throws Exception {
        //提交执行
        Future<String> r1 = executorService.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                return OCRUtils.simpleImg(file);
            }
        });
        //获取结果
        return r1.get();
    }
}
